<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否已读" prop="isRead">
        <el-select v-model="queryParams.isRead" placeholder="请选择" clearable>
          <el-option :key="'0'" label="未读" :value="'0'"/>
          <el-option :key="'1'" label="已读" :value="'1'"/>
        </el-select>
      </el-form-item>
      <el-form-item label="通知类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option :key="'0'" label="评论通知" :value="'0'"/>
          <el-option :key="'1'" label="审核通知" :value="'1'"/>
          <el-option :key="'2'" label="文章通知" :value="'2'"/>
          <el-option :key="'3'" label="系统通知" :value="'3'"/>
        </el-select>
      </el-form-item>
      <!--  因为后端无法直接接收JS Date格式的数据，所以需要使用value-format="yyyy-MM-dd"将Date格式转换成String格式    -->
      <el-form-item label="通知时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          type="date"
          placeholder="选择日期时间"
          align="right"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="通知内容" align="center" prop="content"/>
      <el-table-column label="是否已读" align="center" prop="isRead">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isRead == 0" type="danger">未读</el-tag>
          <el-tag v-if="scope.row.isRead == 1" type="success">已读</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="文章类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 0" type="primary">评论通知</el-tag>
          <el-tag v-if="scope.row.type == 1" type="warning">审核通知</el-tag>
          <el-tag v-if="scope.row.type == 2" type="info">文章通知</el-tag>
          <el-tag v-if="scope.row.type == 3" type="danger">系统通知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="所属用户" align="center" prop="userName"/>
      <el-table-column label="所属文章" align="center" prop="articleName"/>
      <el-table-column label="通知时间" align="center" prop="createTime"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />

    <!-- 添加或修改通知对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="通知内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入通知内容"/>
        </el-form-item>
        <el-form-item v-show="form.type" label="通知类型" prop="typeContent">
          <el-input v-model="form.typeContent" disabled/>
        </el-form-item>
        <el-form-item v-if="form.userName" label="所属用户" prop="userName">
          <el-input
            v-model="form.userName"
            disabled
          />
        </el-form-item>
        <el-form-item v-else label="选择用户" prop="userIds">
          <el-select
            v-model="form.userIds"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入用户名搜索用户"
            :remote-method="remoteMethod"
            :loading="loading2">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.userName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-show="form.articleName" label="所属文章" prop="articleName">
          <el-input v-model="form.articleName" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addNotice, delNotice, exportNotice, getNotice, listNotice, updateNotice} from "@/api/content/notice";
import {searchByUserName} from "@/api/system/user";

let today = new Date(
  Date.UTC(
    new Date().getFullYear(),
    new Date().getMonth(),
    new Date().getDate()
  )
)
export default {
  name: 'Notice',
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      options:[],
      // 遮罩层
      loading: true,
      loading2: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知表格数据
      noticeList: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        isRead: null,
        userId: null,
        articleId: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    remoteMethod(query) {
      if (query !== '') {
        this.loading2 = true;
        searchByUserName(query).then((res)=>{
          this.loading2=false;
          this.options = res
        })
      } else {
        this.options = [];
      }
    },
    /** 查询通知列表 */
    getList() {
      this.loading = true
      listNotice(this.queryParams).then((response) => {
        this.noticeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有通知数据？').then(() => {
        this.exportLoading = true
        return exportNotice()
      }).then(response => {
        // this.$download.name(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
        type: null,
        userId:null,
        userIds:[],
        userName: null,
        articleName: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加通知'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getNotice(id).then((response) => {
        this.form = response
        if (this.form.type == 0){
          this.form.typeContent = "评论通知"
        }else if (this.form.type == 1){
          this.form.typeContent = "审核通知"
        }else if (this.form.type == 2){
          this.form.typeContent = "文章通知"
        }else if (this.form.type == 3){
          this.form.typeContent = "系统通知"
        }
        this.open = true
        this.title = '修改通知内容'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateNotice(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addNotice(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除通知编号为"' + ids + '"的数据项？')
        .then(function () {
          return delNotice(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    }
  }
}
</script>
